<template>
  <el-container class="department-style">
    <el-main>
      <el-card shadow="never" class="commercia-tenant-list-title tableCard">
        <vxe-toolbar :custom="{icon: 'el-icon-s-operation'}" :refresh="{query: getList}" />
        <vxe-table
          ref="xTree"
          border
          stripe
          resizable
          highlight-hover-row
          show-overflow
          :tree-config="{children: 'children', expandAll: true}"
          :row-config="{keyField: id}"
          :loading="loading"
          :auto-resize="true"
          :data="activitiesList"
          height="550"
          :print-config="{}"
        >
          <vxe-table-column tree-node field="name" width="300" title="部门名称" />
          <vxe-table-column field="code" width="150" title="部门代码" />
          <vxe-table-column field="leaderid" width="150" title="部门负责人" />
          <vxe-table-column field="phone" width="150" title="联系电话" />
          <vxe-table-column field="remark" min-width="120" title="备注" />
          <vxe-table-column field="publishStatus" width="80" align="center" title="状态">
            <template slot-scope="scope">
              <el-switch v-show="scope.row.parentId !== '0'" v-model="scope.row.publishStatus" :inactive-value="0" :active-value="1" active-text inactive-text @change="changeState(scope)" />
            </template>
          </vxe-table-column>
          <vxe-table-column
            title="操作"
            fixed="right"
            width="220"
            align="center"
            slots="default"
          >
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="openModal(scope.row.id, 2, true, scope.row.level)">查看</el-button>
              <el-button v-if="scope.row.publishStatus === 0 && scope.row.parentId !== '0'" type="text" size="mini" @click="openModal(scope.row.id, 2, false, scope.row.level)">编辑</el-button>
              <el-button v-if="scope.row.publishStatus === 0 && scope.row.parentId !== '0'" type="text" size="mini" @click="del(scope.row.id)">删除</el-button>
              <el-button type="text" size="mini" @click="openModal(scope.row.id, 1, false, scope.row.level)">添加下级</el-button>
            </template>
          </vxe-table-column>
        </vxe-table>
      </el-card>
    </el-main>
    <el-dialog v-if="dialogVisible" width="45%" title="部门信息" :visible.sync="dialogVisible" :close-on-click-modal="false">
      <el-form ref="ruleForm" :model="params" :rules="rules" label-width="6rem">
        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item prop="code" label="部门代码：">
              <el-input v-model="params.code" :disabled="diabledEdit" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="name" label="部门名称：">
              <el-input v-model="params.name" :disabled="diabledEdit" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="phone" label="部门负责人：">
              <el-input v-model="params.phone" :disabled="diabledEdit" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="leaderid" label="联系电话：">
              <el-input v-model="params.leaderid" :disabled="diabledEdit" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="remark" label="备注：">
              <el-input v-model="params.remark" type="textarea" rows="3" :disabled="diabledEdit" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </el-container>
</template>

<script>
import { delDepartment, getDepartmentListTree, addDepartment, modifyDepartment, viewDepartment, changeDepartmentStatus } from '@/api/org/organize'
export default {
  data() {
    return {
      activitiesList: [],
      loading: false,
      params: {},
      dialogVisible: false,
      diabledEdit: false,
      rules: {
        name: [{ required: true, message: '请输入业态名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入业态编码', trigger: 'blur' }]
      },
      parentId: 0,
      type: 1, // 1 添加 2修改
      operationType: 0 // 0 添加 1 注册
    }
  },
  created() {
    this.getList()
  },
  methods: {
    openModal(id, type, bool, level) {
      this.diabledEdit = bool
      this.operationType = type
      if (type === 2) {
        viewDepartment({ id }).then(response => {
          this.params = response
          this.dialogVisible = true
        })
      } else {
        this.params = {}
        this.params.parentId = id
        this.params.level = level + 1
        this.params.sort = 99
        this.dialogVisible = true
      }
    },
    getList() {
      getDepartmentListTree({
        parentId: 0
      }).then(response => {
        this.activitiesList = response
        this.$refs.xTree.setAllTreeExpand(true)
      })
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.operationType === 1) {
            addDepartment(this.params).then(response => {
              this.$message.success('添加成功')
              this.dialogVisible = false
              this.getList()
            })
          } else {
            modifyDepartment(this.params).then(response => {
              this.$message.success('修改成功')
              this.dialogVisible = false
              this.getList()
            })
          }
        }
      })
    },
    del(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delDepartment(id).then(response => {
          this.$message.success('删除成功')
          this.getList()
        })
      })
    },
    changeState(scope) {
      changeDepartmentStatus({
        id: scope.row.id,
        status: scope.row.publishStatus
      }).then(response => {
        this.$message.success(scope.row.publishStatus === 0 ? '禁用成功' : '启用成功')
      })
    }
  }
}
</script>

<style lang="scss">
.department-style {

}
</style>
